package com.namuan.jp_stock.util;

import java.util.ArrayList;

import com.namuan.jp_stock.object.ShotActivity;
import com.namuan.jp_stock.object.Timestamp;
import com.namuan.jp_stock.R;





import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


public class ActivityItemAdapter extends ArrayAdapter<ShotActivity>
{
	
	private int mResource;
	private ArrayList<ShotActivity> mList;
	private LayoutInflater mInflater;
	private Activity activity;
	public CustomImageManager imageManager;
	/**
	 * @param context
	 * @param layoutResource
	 * @param objects
	 */
	public ActivityItemAdapter(Activity a, int layoutResource, ArrayList<ShotActivity> objects)
	{
		super(a, layoutResource, objects);
		this.activity =a;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageManager = 
			new CustomImageManager(activity.getApplicationContext(),R.drawable.defaultuser);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		
		convertView = mInflater.inflate(mResource, null);
		ProfileImageView activityUserImg=(ProfileImageView) convertView.findViewById(R.id.activityUserImg);
		TextView activityUserNickname = (TextView) convertView.findViewById(R.id.activityUserNickname);
		TextView activityComment = (TextView) convertView.findViewById(R.id.activityComment);
		TextView activitySummary = (TextView) convertView.findViewById(R.id.activitySummary);
		TextView activityTimeStamp = (TextView) convertView.findViewById(R.id.activityTimeStamp);
		
		TextView activityRecommendation = (TextView) convertView.findViewById(R.id.activityRecommendation);
		TextView activityTrackback = (TextView) convertView.findViewById(R.id.activityTrackback);
		

		ShotActivity shotactivity = mList.get(position);
		if (shotactivity.getProfilepic() != null && !shotactivity.getProfilepic().equals("null")) {
			activityUserImg.setTag(Justplace.PROFILEIMG_URL + shotactivity.getProfilepic());
			Log.d("!!!!!!!!!!!!!!!!!!!!!!!!", Justplace.PROFILEIMG_URL + shotactivity.getProfilepic());
			imageManager.displayImage(Justplace.PROFILEIMG_URL + shotactivity.getProfilepic(), activity, activityUserImg);
		} else {
			activityUserImg.setTag(null);
			imageManager.displayImage(shotactivity.getProfilepic(), activity, activityUserImg);
		}
		
		activityUserNickname.setText(shotactivity.getUsernickname());
		String recommendation = shotactivity.getRecommendation();
		String trackbackcount = shotactivity.getTrackbackcount();
		
		// stocktype(코스피, 코스닥)에 따라 액티비티를 구분해줄 것이냐?
		String stocktype = shotactivity.getStocktype();
		
		activityRecommendation.setText(recommendation.toString());
		activityTrackback.setText(trackbackcount.toString());

		if( shotactivity.isMember() == false ) {
			activityUserNickname.setTextColor(R.color.ltgray);
		}
		
		
		activityComment.setText(shotactivity.getComment());
		activitySummary.setText("("+shotactivity.getStockname() + " @ 주식SNS)" );
		activityTimeStamp.setText(new Timestamp().timestmsp(shotactivity.getTimestamp()));
		
		/*************** **********************
		 * *************** **********************
		 * *************** **********************
		 *************** ***********************/
		boolean ismemeber = shotactivity.isMember();
		
		/*************** **********************
		 * *************** **********************
		 * *************** **********************
		 * *************** **********************
		 * *************** **********************/
		
		/*****
		ShotActivityViewHolder viewHolder;
		if(convertView == null) {
			convertView = mInflater.inflate(mResource, null);
			viewHolder = new ShotActivityViewHolder();
			viewHolder.activityUserImg=(ProfileImageView) convertView.findViewById(R.id.activityUserImg);
			viewHolder.activityUserNickname=(TextView) convertView.findViewById(R.id.activityUserNickname);
			viewHolder.activityComment=(TextView) convertView.findViewById(R.id.activityComment);
			viewHolder.activitySummary=(TextView) convertView.findViewById(R.id.activitySummary);
			viewHolder.activityRating=(RatingBar) convertView.findViewById(R.id.activityRating);
			viewHolder.activityTimeStamp=(TextView) convertView.findViewById(R.id.activityTimeStamp);			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ShotActivityViewHolder)convertView.getTag();			
		}
		
		ShotActivity shotactivity = mList.get(position);
		if(shotactivity.getProfilepic()!= null && !shotactivity.getProfilepic().equals("null")){
			Log.d("!!!!!!!!!!!!!!!!!!!!!!!!", Justplace.PROFILEIMG_URL+shotactivity.getProfilepic());
			imageManager.displayImage(Justplace.PROFILEIMG_URL+shotactivity.getProfilepic(),activity, viewHolder.activityUserImg );
	    	
		}else{
			imageManager.displayImage(shotactivity.getProfilepic(),activity, viewHolder.activityUserImg);
		}
		
		viewHolder.activityUserNickname.setText(shotactivity.getUserid());
		viewHolder.activityComment.setText(shotactivity.getComment());
		viewHolder.activitySummary.setText("("+shotactivity.getWinename() + " @" + shotactivity.getStorename() +")" );				
		viewHolder.activityRating.setRating(shotactivity.getRating());
		viewHolder.activityTimeStamp.setText(new Timestamp().timestmsp(shotactivity.getDate()));
		
		
		******/
		return convertView;
	}


	public class ActivityViewHolder {
	  public ProfileImageView activityUserImg;
	  public TextView activityUserNickname;
	  public TextView activityComment;
	  public TextView activitySummary;
	  public RatingBar activityRating;
	  public TextView activityTimeStamp;		
	}
	
}
